package com.runa.monitor.platinterface.netservice.handler;

import com.runa.monitor.platinterface.netservice.server.Server;
import com.runa.monitor.platinterface.base.AnalysisFactory;
import com.runa.protocol.analysis.MetaBufferAnalysis;
import com.runa.protocol.enums.ModbusType;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;

/**
 * @author zhuj
 */
public class PlcChannelInitializer extends ChannelInitializer {

    private MetaBufferAnalysis analysis;

    private ResponseHandler responseHandler;

    public PlcChannelInitializer(ModbusType modbusType, Server server) {
        analysis = AnalysisFactory.create(modbusType);
        responseHandler = new ResponseHandler(server);
    }

    @Override
    protected void initChannel(Channel ch) {
        ch.pipeline().addLast(new ProtocolDecoder(analysis));
        ch.pipeline().addLast(new ProtocolEncoder());
        ch.pipeline().addLast(responseHandler);
    }

}
